<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - sigmoid_cross_entropy_loss_layer.cpp<span style="font-size: 80%;"> (source / <a href="sigmoid_cross_entropy_loss_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">68</td>
            <td class="headerCovTableEntryLo">2.9 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">2</td>
            <td class="headerCovTableEntry">18</td>
            <td class="headerCovTableEntryLo">11.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;algorithm&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;vector&gt;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &quot;caffe/layers/sigmoid_cross_entropy_loss_layer.hpp&quot;
<span class="lineNum">       5 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : namespace caffe {
<span class="lineNum">       8 </span>            : 
<span class="lineNum">       9 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      10 </span><span class="lineNoCov">          0 : void SigmoidCrossEntropyLossLayer&lt;Dtype&gt;::LayerSetUp(</span>
<span class="lineNum">      11 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      12 </span><span class="lineNoCov">          0 :   LossLayer&lt;Dtype&gt;::LayerSetUp(bottom, top);</span>
<span class="lineNum">      13 </span>            :   sigmoid_bottom_vec_.clear();
<span class="lineNum">      14 </span><span class="lineNoCov">          0 :   sigmoid_bottom_vec_.push_back(bottom[0]);</span>
<span class="lineNum">      15 </span>            :   sigmoid_top_vec_.clear();
<span class="lineNum">      16 </span><span class="lineNoCov">          0 :   sigmoid_top_vec_.push_back(sigmoid_output_.get());</span>
<span class="lineNum">      17 </span><span class="lineNoCov">          0 :   sigmoid_layer_-&gt;SetUp(sigmoid_bottom_vec_, sigmoid_top_vec_);</span>
<span class="lineNum">      18 </span>            : 
<span class="lineNum">      19 </span><span class="lineNoCov">          0 :   has_ignore_label_ =</span>
<span class="lineNum">      20 </span>            :     this-&gt;layer_param_.loss_param().has_ignore_label();
<span class="lineNum">      21 </span><span class="lineNoCov">          0 :   if (has_ignore_label_) {</span>
<span class="lineNum">      22 </span><span class="lineNoCov">          0 :     ignore_label_ = this-&gt;layer_param_.loss_param().ignore_label();</span>
<span class="lineNum">      23 </span>            :   }
<span class="lineNum">      24 </span><span class="lineNoCov">          0 :   if (this-&gt;layer_param_.loss_param().has_normalization()) {</span>
<span class="lineNum">      25 </span><span class="lineNoCov">          0 :     normalization_ = this-&gt;layer_param_.loss_param().normalization();</span>
<span class="lineNum">      26 </span><span class="lineNoCov">          0 :   } else if (this-&gt;layer_param_.loss_param().has_normalize()) {</span>
<span class="lineNum">      27 </span><span class="lineNoCov">          0 :     normalization_ = this-&gt;layer_param_.loss_param().normalize() ?</span>
<span class="lineNum">      28 </span>            :                      LossParameter_NormalizationMode_VALID :
<span class="lineNum">      29 </span>            :                      LossParameter_NormalizationMode_BATCH_SIZE;
<span class="lineNum">      30 </span>            :   } else {
<span class="lineNum">      31 </span><span class="lineNoCov">          0 :     normalization_ = LossParameter_NormalizationMode_BATCH_SIZE;</span>
<span class="lineNum">      32 </span>            :   }
<span class="lineNum">      33 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      36 </span><span class="lineNoCov">          0 : void SigmoidCrossEntropyLossLayer&lt;Dtype&gt;::Reshape(</span>
<span class="lineNum">      37 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      38 </span><span class="lineNoCov">          0 :   LossLayer&lt;Dtype&gt;::Reshape(bottom, top);</span>
<span class="lineNum">      39 </span><span class="lineNoCov">          0 :   outer_num_ = bottom[0]-&gt;shape(0);  // batch size</span>
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :   inner_num_ = bottom[0]-&gt;count(1);  // instance size: |output| == |target|</span>
<span class="lineNum">      41 </span><span class="lineNoCov">          0 :   CHECK_EQ(bottom[0]-&gt;count(), bottom[1]-&gt;count()) &lt;&lt;</span>
<span class="lineNum">      42 </span>            :       &quot;SIGMOID_CROSS_ENTROPY_LOSS layer inputs must have the same count.&quot;;
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :   sigmoid_layer_-&gt;Reshape(sigmoid_bottom_vec_, sigmoid_top_vec_);</span>
<span class="lineNum">      44 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      45 </span>            : 
<span class="lineNum">      46 </span>            : // TODO(shelhamer) loss normalization should be pulled up into LossLayer,
<a name="47"><span class="lineNum">      47 </span>            : // instead of duplicated here and in SoftMaxWithLossLayer</a>
<span class="lineNum">      48 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      49 </span><span class="lineNoCov">          0 : Dtype SigmoidCrossEntropyLossLayer&lt;Dtype&gt;::get_normalizer(</span>
<span class="lineNum">      50 </span>            :     LossParameter_NormalizationMode normalization_mode, int valid_count) {
<span class="lineNum">      51 </span>            :   Dtype normalizer;
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :   switch (normalization_mode) {</span>
<span class="lineNum">      53 </span>            :     case LossParameter_NormalizationMode_FULL:
<span class="lineNum">      54 </span><span class="lineNoCov">          0 :       normalizer = Dtype(outer_num_ * inner_num_);</span>
<span class="lineNum">      55 </span><span class="lineNoCov">          0 :       break;</span>
<span class="lineNum">      56 </span>            :     case LossParameter_NormalizationMode_VALID:
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :       if (valid_count == -1) {</span>
<span class="lineNum">      58 </span><span class="lineNoCov">          0 :         normalizer = Dtype(outer_num_ * inner_num_);</span>
<span class="lineNum">      59 </span>            :       } else {
<span class="lineNum">      60 </span><span class="lineNoCov">          0 :         normalizer = Dtype(valid_count);</span>
<span class="lineNum">      61 </span>            :       }
<span class="lineNum">      62 </span>            :       break;
<span class="lineNum">      63 </span>            :     case LossParameter_NormalizationMode_BATCH_SIZE:
<span class="lineNum">      64 </span><span class="lineNoCov">          0 :       normalizer = Dtype(outer_num_);</span>
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :       break;</span>
<span class="lineNum">      66 </span>            :     case LossParameter_NormalizationMode_NONE:
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :       normalizer = Dtype(1);</span>
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :       break;</span>
<span class="lineNum">      69 </span>            :     default:
<span class="lineNum">      70 </span><span class="lineNoCov">          0 :       LOG(FATAL) &lt;&lt; &quot;Unknown normalization mode: &quot;</span>
<span class="lineNum">      71 </span>            :           &lt;&lt; LossParameter_NormalizationMode_Name(normalization_mode);
<span class="lineNum">      72 </span>            :   }
<span class="lineNum">      73 </span>            :   // Some users will have no labels for some examples in order to 'turn off' a
<span class="lineNum">      74 </span>            :   // particular loss in a multi-task setup. The max prevents NaNs in that case.
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :   return std::max(Dtype(1.0), normalizer);</span>
<span class="lineNum">      76 </span>            : }
<a name="77"><span class="lineNum">      77 </span>            : </a>
<span class="lineNum">      78 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      79 </span><span class="lineNoCov">          0 : void SigmoidCrossEntropyLossLayer&lt;Dtype&gt;::Forward_cpu(</span>
<span class="lineNum">      80 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      81 </span>            :   // The forward pass computes the sigmoid outputs.
<span class="lineNum">      82 </span><span class="lineNoCov">          0 :   sigmoid_bottom_vec_[0] = bottom[0];</span>
<span class="lineNum">      83 </span><span class="lineNoCov">          0 :   sigmoid_layer_-&gt;Forward(sigmoid_bottom_vec_, sigmoid_top_vec_);</span>
<span class="lineNum">      84 </span>            :   // Compute the loss (negative log likelihood)
<span class="lineNum">      85 </span>            :   // Stable version of loss computation from input data
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :   const Dtype* input_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :   const Dtype* target = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">      88 </span>            :   int valid_count = 0;
<span class="lineNum">      89 </span>            :   Dtype loss = 0;
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; bottom[0]-&gt;count(); ++i) {</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :     const int target_value = static_cast&lt;int&gt;(target[i]);</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :     if (has_ignore_label_ &amp;&amp; target_value == ignore_label_) {</span>
<span class="lineNum">      93 </span>            :       continue;
<span class="lineNum">      94 </span>            :     }
<span class="lineNum">      95 </span><span class="lineNoCov">          0 :     loss -= input_data[i] * (target[i] - (input_data[i] &gt;= 0)) -</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :         log(1 + exp(input_data[i] - 2 * input_data[i] * (input_data[i] &gt;= 0)));</span>
<span class="lineNum">      97 </span><span class="lineNoCov">          0 :     ++valid_count;</span>
<span class="lineNum">      98 </span>            :   }
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :   normalizer_ = get_normalizer(normalization_, valid_count);</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :   top[0]-&gt;mutable_cpu_data()[0] = loss / normalizer_;</span>
<span class="lineNum">     101 </span><span class="lineNoCov">          0 : }</span>
<a name="102"><span class="lineNum">     102 </span>            : </a>
<span class="lineNum">     103 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">     104 </span><span class="lineNoCov">          0 : void SigmoidCrossEntropyLossLayer&lt;Dtype&gt;::Backward_cpu(</span>
<span class="lineNum">     105 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top, const vector&lt;bool&gt;&amp; propagate_down,
<span class="lineNum">     106 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom) {
<span class="lineNum">     107 </span><span class="lineNoCov">          0 :   if (propagate_down[1]) {</span>
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :     LOG(FATAL) &lt;&lt; this-&gt;type()</span>
<span class="lineNum">     109 </span>            :                &lt;&lt; &quot; Layer cannot backpropagate to label inputs.&quot;;
<span class="lineNum">     110 </span>            :   }
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :   if (propagate_down[0]) {</span>
<span class="lineNum">     112 </span>            :     // First, compute the diff
<span class="lineNum">     113 </span><span class="lineNoCov">          0 :     const int count = bottom[0]-&gt;count();</span>
<span class="lineNum">     114 </span><span class="lineNoCov">          0 :     const Dtype* sigmoid_output_data = sigmoid_output_-&gt;cpu_data();</span>
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :     const Dtype* target = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :     Dtype* bottom_diff = bottom[0]-&gt;mutable_cpu_diff();</span>
<span class="lineNum">     117 </span><span class="lineNoCov">          0 :     caffe_sub(count, sigmoid_output_data, target, bottom_diff);</span>
<span class="lineNum">     118 </span>            :     // Zero out gradient of ignored targets.
<span class="lineNum">     119 </span><span class="lineNoCov">          0 :     if (has_ignore_label_) {</span>
<span class="lineNum">     120 </span><span class="lineNoCov">          0 :       for (int i = 0; i &lt; count; ++i) {</span>
<span class="lineNum">     121 </span><span class="lineNoCov">          0 :         const int target_value = static_cast&lt;int&gt;(target[i]);</span>
<span class="lineNum">     122 </span><span class="lineNoCov">          0 :         if (target_value == ignore_label_) {</span>
<span class="lineNum">     123 </span><span class="lineNoCov">          0 :           bottom_diff[i] = 0;</span>
<span class="lineNum">     124 </span>            :         }
<span class="lineNum">     125 </span>            :       }
<span class="lineNum">     126 </span>            :     }
<span class="lineNum">     127 </span>            :     // Scale down gradient
<span class="lineNum">     128 </span><span class="lineNoCov">          0 :     Dtype loss_weight = top[0]-&gt;cpu_diff()[0] / normalizer_;</span>
<span class="lineNum">     129 </span><span class="lineNoCov">          0 :     caffe_scal(count, loss_weight, bottom_diff);</span>
<span class="lineNum">     130 </span>            :   }
<span class="lineNum">     131 </span><span class="lineNoCov">          0 : }</span>
<a name="132"><span class="lineNum">     132 </span>            : </a>
<span class="lineNum">     133 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     134 </span><span class="lineNoCov">          0 : STUB_GPU(SigmoidCrossEntropyLossLayer);</span>
<span class="lineNum">     135 </span>            : #endif
<a name="136"><span class="lineNum">     136 </span>            : </a>
<span class="lineNum">     137 </span>            : INSTANTIATE_CLASS(SigmoidCrossEntropyLossLayer);
<a name="138"><span class="lineNum">     138 </span><span class="lineCov">          3 : REGISTER_LAYER_CLASS(SigmoidCrossEntropyLoss);</span></a>
<span class="lineNum">     139 </span>            : 
<span class="lineNum">     140 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
